﻿jQuery(function() {
	function getFormInfo(inlayout, inform) {

		var html = jQuery.ajax({
			url : 'index.php',
			type : 'POST',
			async : true,
			data : {
				task : 'empformview',
				format : 'raw',
				option : 'com_wrecruit',
				c : 'getform',
				view : 'employer',
				layout : inlayout,
				form : inform
			},
			dataType : "html",
			async : false,
			global : false,
			success : function(msg) {
				return msg;

			}
		}).responseText;

		return html;
	}

	// form job preview

	function jobPreviewInfo(inlayout, inform,jobtitle,industry,careerlevel,shift, deadline,description,compdescription, applicable, agefrom, ageto, educationid, requiredtravel, 
			minexperiencerange, maxexperiencerange, experiencetext, qualifications,salaryrangefrom, morebenefit,startpublishing ) {

		var html = jQuery.ajax({
			url : 'index.php',
			type : 'POST',
			async : true,
			data : {
				task : 'empformview',
				format : 'raw',
				option : 'com_wrecruit',
				c : 'getform',
				view : 'employer',
				layout : inlayout,
				form : inform,
				jobtitle: jobtitle,
				industry: industry,
				careerlevel: careerlevel,
				shift: shift,
				deadline: deadline,
				description: description,
				compdescription: compdescription,
				applicable:applicable,
				gender: gender,
				agefrom: agefrom,
				ageto: ageto,
				educationid: educationid,
				requiredtravel: requiredtravel,
				minexperiencerange: minexperiencerange,
				maxexperiencerange: maxexperiencerange,
				experiencetext: experiencetext,
				qualifications: qualifications,
				salaryrangefrom: salaryrangefrom,
				morebenefit: morebenefit,
				startpublishing: startpublishing
			},
			dataType : "html",
			async : false,
			global : false,
			success : function(msg) {
				return msg;

			}
		}).responseText;

		return html;
	}

	// end job preview
	
	//all form

	function FormInfo(inlayout, inform, name, email,candidateid, employerid) {

		var html = jQuery.ajax({
			url : 'index.php',
			type : 'POST',
			async : true,
			data : {
				task : 'empformview',
				format : 'raw',
				option : 'com_wrecruit',
				c : 'getform',
				view : 'employer',
				layout : inlayout,
				form : inform,
				candidatename: name,
				candidateemail:email,
				cid : candidateid,
				empid : employerid
			},
			dataType : "html",
			async : false,
			global : false,
			success : function(msg) {
				return msg;

			}
		}).responseText;

		return html;
	}
	
	//Form edit
	function setFormEdit(inlayout, inform, empemail,uid) {

		var html = jQuery.ajax({
			url : 'index.php',
			type : 'POST',
			async : true,
			data : {
				task : 'empformview',
				format : 'raw',
				option : 'com_wrecruit',
				c : 'getform',
				view : 'employer',
				layout : inlayout,
				form : inform,
				email:empemail,
				empuid : uid
			},
			dataType : "html",
			async : false,
			global : false,
			success : function(msg) {
				return msg;

			}
		}).responseText;

		return html;
	}
	
	
	

	// Get Form name
	function getFormName(me) {
		var id = jQuery(me).attr('ID');
		htmlId = id;
		var form = "";
		switch (id) {

		case "email":
			form = 'emailform';
			break;
		case "password":
			form = 'passwordform';
			break;

		case "jobpreview":
			form = 'jobpreview';
			break;
		
		case "sendmessage":
			form = 'sendmessage';
			break;
		case "replymessage": case "replymessage2":
			form = 'replymessage';
			break;
		case "forwardmessage": case "forwardmessage2":
			form = 'forwardmessage';
			break;
			
		case "request_cv":
			form = 'request_cv';
			break;

		default:
			form = "formwarning";

		}
		return form;
	}

	jQuery("#close").click(function() {
		hidePopup();
	});

	jQuery("#overlayEffect").click(function() {
		hidePopup();
	});

	
	var popup = false;
	jQuery(".wpopup").mouseover(
	           function()
	           { 
				// jQuery(this).css('text-decoration','underline');
	           }  
	);
	jQuery(".wpopup").mouseout(
	           function()
	           {  
				 jQuery(this).css('text-decoration','none');
	           }  
	);
	//Get add popup
	jQuery(".wpopup").click(function(e){
		var uid = jQuery('#uid').attr('name');
		var email = jQuery('#hemail').attr('name');
		var formType = getFormName(jQuery(this));
		jQuery('#popupContainer').html(setFormEdit('popuppages', formType, email, uid));		
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;
		}
	});
	
	
	//job preview
	jQuery(".wepopup").click(function() {
		popup = false;
		var jobtitle = jQuery('#title').val();
		alert("jobtitle:"+jobtitle);
		var industry = jQuery('#jobcategory').val();
		var careerlevel = jQuery('#careerlevel').val();
		var shift = jQuery('#careerlevel').val();
		var deadline = jQuery('#careerlevel').val();
		var description = jQuery('#careerlevel').val();
		var compdescription = jQuery('#careerlevel').val();
		var applicable= jQuery('#careerlevel').val();
		var gender = jQuery('#careerlevel').val();
		var agefrom = jQuery('#careerlevel').val(); 
		var ageto = jQuery('#careerlevel').val();
		var educationid = jQuery('#careerlevel').val();
		var requiredtravel = jQuery('#careerlevel').val();
		var minexperiencerange = jQuery('#careerlevel').val();
		var maxexperiencerange = jQuery('#careerlevel').val();
		var experiencetext = jQuery('#careerlevel').val();
		var qualifications = jQuery('#careerlevel').val();
		var salaryrangefrom = jQuery('#careerlevel').val();
		var morebenefit = jQuery('#careerlevel').val();
		var startpublishing = jQuery('#careerlevel').val();
		
		
		var formType = getFormName(jQuery(this));
		jQuery('#popupContainer').html(jobPreviewInfo('popuppages', formType,jobtitle,industry, careerlevel, shift,  deadline, description,compdescription,
				applicable,	gender,agefrom, ageto, educationid, requiredtravel,	minexperiencerange,maxexperiencerange, experiencetext,qualifications,salaryrangefrom, morebenefit,startpublishing ));
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;

		}
	});
	//end job preview
	
	jQuery(".sendmsg").click(function() {
		popup = false;
		var child_input = jQuery(this).children('input');
		var name = child_input[0].value;
		var email = child_input[1].value;
		var cid = child_input[2].value;
		var empid = child_input[3].value;
	
		var formType = getFormName(jQuery(this));	
		jQuery('#popupContainer').html(FormInfo('popuppages', formType, name, email, cid, empid));
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;

		}
	});
	
	jQuery(".replymessage").click(function() {
		popup = false; 
		var name = jQuery('#cname').attr('name')+','+jQuery('#subject').attr('name');
		var email = jQuery('#email').attr('name');
		var cid = jQuery('#cid').attr('name');
		var empid = jQuery('#empid').attr('name');
	
		var formType = getFormName(jQuery(this));	
		jQuery('#popupContainer').html(FormInfo('popuppages', formType, name, email, cid, empid));
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;

		}
	});
	
	//reply from message list
	jQuery(".reply").click(function() {
		popup = false; 
		var name = jQuery('#cname').attr('name')+','+jQuery('#subject').attr('name');
		var email = jQuery('#email').attr('name');
		var cid = jQuery('#cid').attr('name');
		var empid = jQuery('#empid').attr('name');
	
		var formType = 'replymessage';	
		jQuery('#popupContainer').html(FormInfo('popuppages', formType, name, email, cid, empid));
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;

		}
	});
	
	//forward from message list
	jQuery(".forward").click(function() {
		popup = false; 
		var name = jQuery('#cname').attr('name')+','+jQuery('#subject').attr('name');
		var email = jQuery('#email').attr('name');
		var cid = jQuery('#cid').attr('name');
		var empid = jQuery('#empid').attr('name');
	
		var formType = 'forwardmessage';	
		jQuery('#popupContainer').html(FormInfo('popuppages', formType, name, email, cid, empid));
		if (popup == false) {
			jQuery("#overlayEffect").fadeIn("slow");
			jQuery("#popupContainer").fadeIn("slow");
			jQuery("#close").fadeIn("slow");
			center();
			popup = true;

		}
	});
	
	
	
	

	function center() {
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = jQuery("#popupContainer").height();
		var popupWidth = jQuery("#popupContainer").width();
		jQuery("#popupContainer").css({
			"position" : "absolute",
			"top" : windowHeight,
			"left" : windowWidth / 2 - popupWidth / 2
		});
	}
	function hidePopup() {
		if (popup == true) {
			jQuery("#overlayEffect").fadeOut("slow");
			jQuery("#popupContainer").fadeOut("slow");
			jQuery("#close").fadeOut("slow");
			popup = false;
		}
	}

}, jQuery);
